// ==UserScript==
// @name         textlink自动刷新
// @namespace    http://gitee.com/adogs/tampermonkey-scripts/
// @version      1.1.0
// @updateURL    https://gitee.com/adogs/tampermonkey-scripts/raw/master/textlink%E8%87%AA%E5%8A%A8%E5%88%B7%E6%96%B0.js
// @downloadURL https://gitee.com/adogs/tampermonkey-scripts/raw/master/textlink%E8%87%AA%E5%8A%A8%E5%88%B7%E6%96%B0.js
// @description  try to take over the world!
// @author       adogs
// @match        http://192.168.0.3/testlink/index.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=0.3
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    // 创建主悬浮按钮
    const floatBtn = document.createElement('button');
    floatBtn.id = 'floating-button';
    floatBtn.innerHTML = '⚡';
    floatBtn.style.cssText = `
                position: fixed;
                bottom: 30px;
                right: 30px;
                width: 60px;
                height: 60px;
                border-radius: 50%;
                background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
                color: white;
                border: none;
                font-size: 24px;
                cursor: pointer;
                box-shadow: 0 4px 15px rgba(0,0,0,0.2);
                z-index: 9999;
                transition: all 0.3s ease;
            `;
    // 添加悬停效果
    floatBtn.addEventListener('mouseenter', () => {
        floatBtn.style.transform = 'scale(1.1)';
        floatBtn.style.boxShadow = '0 6px 20px rgba(0,0,0,0.3)';
    });
    floatBtn.addEventListener('mouseleave', () => {
        floatBtn.style.transform = 'scale(1)';
        floatBtn.style.boxShadow = '0 4px 15px rgba(0,0,0,0.2)';
    });
    floatBtn.addEventListener('click', () => {
        if (actionButtons.style.display === 'flex') {
            actionButtons.style.display = 'none';
        } else {
            actionButtons.style.display = 'flex';
        }
    });
    document.body.appendChild(floatBtn);

    // 创建功能按钮容器
    const actionButtons = document.createElement('div');
    actionButtons.id = 'action-buttons';
    actionButtons.style.cssText = `
            position: fixed;
            bottom: 100px;
            right: 30px;
            display: none;
            flex-direction: column;
            gap: 10px;
            z-index: 10000;
            flex-wrap: wrap;
            height: 20%;
        `;
    document.body.appendChild(actionButtons);

    var timers = {
        autoFreshTitleBar: undefined,
    };

    // 功能按钮数据
    const buttons = [
        { icon: '🔃', title: '自动刷新顶部bar', id: 'auto-fresh-title-bar', func: autoFreshTitleBarAction, args: [timers] }
    ];

    // 创建功能按钮
    buttons.forEach((btn, index) => {
        const actionBtn = document.createElement('button');
        actionBtn.className = 'action-btn';
        actionBtn.innerHTML = btn.icon;
        actionBtn.title = btn.title;
        actionBtn.dataset.id = btn.id;
        actionBtn.style.setProperty('--i', index);
        actionButtons.appendChild(actionBtn);
        actionBtn.style.background = 'blue';
        actionBtn.addEventListener('click', function () {
            btn.func.apply(null, [this, ...btn.args])
        });
    });

    // 悬浮按钮点击事件

    function autoFreshTitleBarAction(btn, timers) {
        if (timers.autoFreshTitleBar === undefined) {
            btn.title = "关闭自动刷新顶部bar";
            btn.style.background = 'red';
            timers.autoFreshTitleBar = setInterval(() => {
                document.getElementsByName("titlebar")[0].contentWindow.location.reload();
                console.log("refresh");
            }, 30 * 1000);
        }
        else {
            clearInterval(timers.autoFreshTitleBar);
            timers.autoFreshTitleBar = undefined;
            btn.title = "自动刷新顶部bar";
            btn.style.background = 'blue';
        }
    }
})();